using System;
using System.Collections.Generic;
using Blog.Core.Entities;
using Blog.Infrastructure.DataAcces.Repositories.Abstractisations;
using Blog.Infrastructure.DataAcces.Services.Abstractisations;

namespace Blog.Infrastructure.DataAcces.Services.Implementations
{
    public class ArticleService : IArticleService
    {
        protected IRepository<Article> repository;
        protected IArticleRepository articleRepository;
      

        private int ArticlesPerPage = 3;

        public ArticleService(IRepository<Article> repository, 
                              IArticleRepository articleRepository)
        {
            this.articleRepository = articleRepository;
            this.repository = repository;
            
        }


        public IList<Article> GetAll()
        {
            return repository.GetAll();
        }

        public IList<Article> GetAll(List<long> ids)
        {
            return repository.GetAll(ids);
        }

        public Article Get(long id)
        {
            return repository.Get(id);
        }
        public Article Get(string title)
        {
            return repository.Get(title);
        }
        public void Save(Article item)
        {
            repository.Save(item);
        }

        public void Delete(long id)
        {
            repository.Delete(id);
        }

        public IList<Article> GetLatest(long itemsNumber)
        {
            return articleRepository.Latest(itemsNumber);
        }

        public IList<Article> GetMostPopular(long itemsNumber)
        {
            return articleRepository.MostPopular(itemsNumber);
        }

        public IList<Article> ByCategory(string cat)
        {
            return articleRepository.ByCategory(cat);
        }

        public IList<Category> Categories()
        {
            return articleRepository.Categories();
        }

        public IList<Article> Archive(int page, int range)
        {
            return articleRepository.Archive(page, range);
        }

        public int Count()
        {
            return repository.Count();
        }

        public int PagesInArchive()
        {
            var nrOfArticles = Count();
            return (nrOfArticles / ArticlesPerPage) + (nrOfArticles % ArticlesPerPage != 0 ? 1 : 0);
        }

        public Article GetForView(string title)
        {
            var art = articleRepository.Get(title);
            if (art!=null)
            {
                art.Views++;
                articleRepository.Save(art);
            }
           
            return art;
        }
    }
}